﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Evaluation3.data;
using System.Data.OleDb;
using Evaluation3.classes;
using System.Web.Configuration;



public partial class uicontrol_Nhap_Cac_Chi_Tieu : System.Web.UI.UserControl
{

    OleDbConnection myConnection;
    remoteUser objRemoteUser;
    String MaSanPham = "";
    String MaHoiDong = "";
    DataRow dr;
    int identity = 0;
    //----------------
    //ChiTietThongTin objChiTietThongTin;
    ChiTietThongTin objChiTietThongTin_Temp;
    ChiTietThongTin objCTTT;

    //--------------
    //ChiTietToChuc objChiTietToChuc;
    ChiTietToChuc objChiTietToChuc_Temp;
    ChiTietToChuc objCTTC;
    //------------

    NhanToMoiTruong objNhanToMoiTruong_Temp;
    NhanToMoiTruong objNTMT;
    //------------

    BuocGiaCong objBuocGiaCong_Temp;
    BuocGiaCong objBGC;
    //------------
     ThietBi objThietBi;
    //------------
    HeSoTrongLuong objHeSoTrongLuong_Temp;
    HeSoTrongLuong objHSTL;
    //------------
   

    protected void Page_Load(object sender, EventArgs e)
    {
        String connectinString = WebConfigurationManager.ConnectionStrings["Evaluation"].ConnectionString;
        myConnection = new OleDbConnection(connectinString);
        objRemoteUser = (remoteUser)Session["remoteUser"];
        MaSanPham = (String) Session["MaSanPham"];
        MaHoiDong = (String) Session["MaHoiDong"];
        //if (!IsPostBack)
        //{
            //objChiTietThongTin_Temp = new ChiTietThongTin();
            //objChiTietThongTin_Temp.ActiveConnection = myConnection;
            //objChiTietThongTin_Temp.SelectCondition = "MaSanPham='" + MaSanPham + "' and MaHoiDong='" + MaHoiDong + "'";
            //objChiTietThongTin_Temp.LoadData();
            //Session["objChiTietThongTin_Temp"] = objChiTietThongTin_Temp;
        //}
            if (!IsPostBack)
            {
                LoadDataToGrid();
                LoadDataToGrid_TOCHUC();
                LoadDataToGrid_MOITRUONG();
                LoadDataToCboMaThietBi();
                LoadDataToGrid_HESOTRONGLUONG();
            }
        
    }

  private void LoadDataToGrid()
  {
    //objChiTietThongTin = new ChiTietThongTin();
    //objChiTietThongTin.ActiveConnection = myConnection;
    //objChiTietThongTin.SelectCondition = "MaSanPham='" + MaSanPham + "' and MaHoiDong='" + MaHoiDong + "'";
    //objChiTietThongTin.LoadData();
    //GridView_ChiTietThongTin.DataSource = objChiTietThongTin;
    //GridView_ChiTietThongTin.DataBind();

    objChiTietThongTin_Temp = new ChiTietThongTin();
    objChiTietThongTin_Temp.ActiveConnection = myConnection;
    objChiTietThongTin_Temp.SelectCondition = "MaSanPham='" + MaSanPham + "' and MaHoiDong='" + MaHoiDong + "'";
    objChiTietThongTin_Temp.LoadData();
    if (Session["objChiTietThongTin_Temp"] != null)
    {
        Session["objChiTietThongTin_Temp"] = null;
    }
    Session["objChiTietThongTin_Temp"] = objChiTietThongTin_Temp;
    GridView_ChiTietThongTin.DataSource = (ChiTietThongTin)Session["objChiTietThongTin_Temp"];
    GridView_ChiTietThongTin.DataBind();
  }

    private void LoadDataToGrid_TOCHUC()
    {
        //objChiTietToChuc = new ChiTietToChuc();
        //objChiTietToChuc.ActiveConnection = myConnection;
        //objChiTietToChuc.SelectCondition = "MaSanPham='" + MaSanPham + "' and MaHoiDong='" + MaHoiDong + "'";
        //objChiTietToChuc.LoadData();
        //GridView_ChiTietToChuc.DataSource = objChiTietToChuc;
        //GridView_ChiTietToChuc.DataBind();

        objChiTietToChuc_Temp = new ChiTietToChuc();
        objChiTietToChuc_Temp.ActiveConnection = myConnection;
        objChiTietToChuc_Temp.SelectCondition = "MaSanPham='" + MaSanPham + "' and MaHoiDong='" + MaHoiDong + "'";
        objChiTietToChuc_Temp.LoadData();
        if (Session["objChiTietToChuc_Temp"] != null)
        {
            Session["objChiTietToChuc_Temp"] = null;
        }
        Session["objChiTietToChuc_Temp"] = objChiTietToChuc_Temp;
        GridView_ChiTietToChuc.DataSource = (ChiTietToChuc)Session["objChiTietToChuc_Temp"];
        GridView_ChiTietToChuc.DataBind();
    }

    private void LoadDataToGrid_MOITRUONG()
    {

        objNhanToMoiTruong_Temp = new NhanToMoiTruong();
        objNhanToMoiTruong_Temp.ActiveConnection = myConnection;
        objNhanToMoiTruong_Temp.SelectCondition = "MaSanPham='" + MaSanPham + "' and MaHoiDong='" + MaHoiDong + "'";
        objNhanToMoiTruong_Temp.LoadData();
        if (Session["objNhanToMoiTruong_Temp"] != null)
        {
            Session["objNhanToMoiTruong_Temp"] = null;
        }
        Session["objNhanToMoiTruong_Temp"] = objNhanToMoiTruong_Temp;
        GridView_NhanToMoiTruong.DataSource = (NhanToMoiTruong)Session["objNhanToMoiTruong_Temp"];
        GridView_NhanToMoiTruong.DataBind();
    }


  private void LoadDataToCboMaThietBi()
  {
    objThietBi = new ThietBi();
    objThietBi.ActiveConnection = myConnection;
    objThietBi.SelectCondition= "MaDoanhNghiep='" + objRemoteUser.companyID.ToString() + "'";
    objThietBi.LoadData();
    cboMaThietBi.DataSource = objThietBi;
    cboMaThietBi.DataTextField = "TenThietBi";
    cboMaThietBi.DataValueField = "MaThietBi";
    cboMaThietBi.DataBind();
    cboMaThietBi.Items.Insert(0, "Chọn một thiết bị");
  }
  private void LoadDataToGrid_BUOCGIACONG(string MaThietBi)
    {

      objBuocGiaCong_Temp = new BuocGiaCong();
      objBuocGiaCong_Temp.ActiveConnection = myConnection;
      objBuocGiaCong_Temp.SelectCondition = "MaSanPham='" + MaSanPham + "' and MaHoiDong='" + MaHoiDong + "' and MaThietBi='" + MaThietBi + "'";
      objBuocGiaCong_Temp.LoadData();
      if (Session["objBuocGiaCong_Temp"] != null)
      {
        Session["objBuocGiaCong_Temp"] = null;
      }
      Session["objBuocGiaCong_Temp"] = objBuocGiaCong_Temp;
      GridView_BuocGiaCong.DataSource = (BuocGiaCong)Session["objBuocGiaCong_Temp"];
      GridView_BuocGiaCong.DataBind();
    }

  private void LoadDataToGrid_HESOTRONGLUONG()
  {

    objHeSoTrongLuong_Temp = new HeSoTrongLuong();
    objHeSoTrongLuong_Temp.ActiveConnection = myConnection;
    objHeSoTrongLuong_Temp.SelectCondition = "MaHoiDong='" + MaHoiDong + "'";
    objHeSoTrongLuong_Temp.LoadData();
    if (Session["objHeSoTrongLuong_Temp"] != null)
    {
      Session["objHeSoTrongLuong_Temp"] = null;
    }
    Session["objHeSoTrongLuong_Temp"] = objHeSoTrongLuong_Temp;
    GridView_HeSoTrongLuong.DataSource = (HeSoTrongLuong)Session["objHeSoTrongLuong_Temp"];
    GridView_HeSoTrongLuong.DataBind();
  }


    protected void bntTiepTuc_Click(object sender, EventArgs e)
    {
        if (ASPxPageControl1.ActiveTabIndex == 0)
        {
            ASPxPageControl1.ActiveTabIndex = 1;
        }
        else if (ASPxPageControl1.ActiveTabIndex == 1)
        {
            ASPxPageControl1.ActiveTabIndex = 2;
        }
        else if (ASPxPageControl1.ActiveTabIndex == 2)
        {
            ASPxPageControl1.ActiveTabIndex = 3;
        }
        else if (ASPxPageControl1.ActiveTabIndex == 3)
        {
            ASPxPageControl1.ActiveTabIndex = 4;
        }
    }
  protected void bntCapNhat_Click(object sender, EventArgs e)
  {
    try
    {
      if (txtTenChiTietThongTin.Text == null || txtTenChiTietThongTin.Text.Trim().Length < 1)
      {
        lblTenNhanToMoiTruong.Text = "* Tên chi tiết thông tin không được để trống!";
        lblTenNhanToMoiTruong.Visible = true;
        txtTenChiTietThongTin.Focus();
        return;
      }
      else
      {
        lblTenNhanToMoiTruong.Visible = false;
      }
      if (txtAlphaI.Text != null)
      {
        try
        {
          double I = 0;
          I = Convert.ToDouble(txtAlphaI.Text);
          if (I < 0 || I > 1)
          {
            lblAlphaI.Text = "* Điểm Alpha I phải trong khoảng từ 0 đến 1!";
            lblAlphaI.Visible = true;
            txtAlphaI.Focus();
            return;
          }
          else
          {
            lblAlphaI.Visible = false;
          }
        }
        catch (Exception eConvert)
        {
          lblAlphaI.Text = "* Điểm Alpha I phải là kiểu số!";
          lblAlphaI.Visible = true;
          txtAlphaI.Focus();
          //Console.WriteLine(eConvert.Message);
          return;

        }

      }
      else if (txtAlphaI.Text == null)
      {
        lblAlphaI.Text = "* Điểm Alpha I không được để trống!";
        lblAlphaI.Visible = true;
        txtAlphaI.Focus();
        return;
      }
      else
      {
        lblAlphaI.Visible = false;
      }
      if (txtMaChiTietThongTin.Text.ToString().Trim().Length > 0 && Convert.ToInt32(txtMaChiTietThongTin.Text) > 0)
      {
        objCTTT = (ChiTietThongTin)Session["objChiTietThongTin_Temp"];
        dr = objCTTT.FindByPrimaryKey(txtMaChiTietThongTin.Text.ToString());
        //dr["MaChiTietThongTin"]=
        dr["TenChiTietThongTin"] = txtTenChiTietThongTin.Text;
        dr["AlphaI"] = txtAlphaI.Text;
        dr["GhiChu"] = txtGhiChu.Text;
        objCTTT.SaveData(Evaluation3.data.MyDataTable.DataTableAction.AllChanges);
      }
      if (txtMaChiTietThongTin.Text.ToString().Trim().Length > 0 && Convert.ToInt32(txtMaChiTietThongTin.Text) < 0)
      {
        identity -= 1;
        objCTTT = (ChiTietThongTin)Session["objChiTietThongTin_Temp"];
        dr = objCTTT.NewRow();
        dr["MaChiTietThongTin"] = identity;
        dr["TenChiTietThongTin"] = txtTenChiTietThongTin.Text;
        dr["AlphaI"] = txtAlphaI.Text;
        dr["MaSanPham"] = MaSanPham;
        dr["MaHoiDong"] = MaHoiDong;
        dr["GhiChu"] = txtGhiChu.Text;
        objCTTT.Rows.Add(dr);
        objCTTT.SaveData(Evaluation3.data.MyDataTable.DataTableAction.AllChanges);
        //Session["objChiTietThongTin_Temp"] = objCTTT;

      }

      LoadDataToGrid();
      txtMaChiTietThongTin.Text = "";
      txtTenChiTietThongTin.Text = "";
      txtAlphaI.Text = "";
      txtGhiChu.Text = "";
      txtTenChiTietThongTin.Enabled = false;
      txtAlphaI.Enabled = false;
      txtGhiChu.Enabled = false;
    }
    catch (Exception eRow2)
    {

    }
  }


 


   
    protected void GridView_ChiTietThongTin_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //if (e.CommandName == "Select")
        //{
      try
      {
        int selectedrow = Convert.ToInt32(e.CommandArgument);
        objCTTT = (ChiTietThongTin)Session["objChiTietThongTin_Temp"];
        dr = objCTTT.FindByPrimaryKey(GridView_ChiTietThongTin.Rows[selectedrow].Cells[0].Text.ToString());
        txtMaChiTietThongTin.Text = dr["MaChiTietThongTin"].ToString();
        txtTenChiTietThongTin.Text = dr["TenChiTietThongTin"].ToString();
        txtAlphaI.Text = dr["AlphaI"].ToString();
        txtGhiChu.Text = dr["GhiChu"].ToString();
        txtTenChiTietThongTin.Enabled = true;
        txtAlphaI.Enabled = true;
        txtGhiChu.Enabled = true;
      }
      catch (Exception eRow1)
      {

      }
        //    //////int selectedrow = Convert.ToInt32(GridView_ChiTietThongTin.SelectedIndex);
        //    ////int selectedrow = Convert.ToInt32(e.CommandArgument);
        //    //////txtTenChiTietThongTin.Text = GridView_ChiTietThongTin.Rows[selectedrow].Cells[1].;
        //    ////txtMaChiTietToChuc.Value = GridView_ChiTietThongTin.Rows[selectedrow].Cells[0].Text.ToString();
        //    ////txtTenChiTietThongTin.Text = GridView_ChiTietThongTin.Rows[selectedrow].Cells[1].Text.ToString();
        //    ////txtAlphaI.Text = GridView_ChiTietThongTin.Rows[selectedrow].Cells[4].Text.ToString();
        //    ////txtGhiChu.Text=  GridView_ChiTietThongTin.Rows[selectedrow].Cells[5].Text.ToString();

        //}
        //else if (e.CommandName == "Delete")
        //{
        //    int selectedrow = Convert.ToInt32(e.CommandArgument);
        //    objCTTT = (ChiTietThongTin)Session["objChiTietThongTin_Temp"];
        //    dr = objCTTT.FindByPrimaryKey(GridView_ChiTietThongTin.Rows[selectedrow].Cells[0].Text.ToString());
        //    dr.Delete();
        //    objCTTT.SaveData(Evaluation3.data.MyDataTable.DataTableAction.Delete);
        //   // GridView_ChiTietThongTin.DeleteRow(selectedrow);
        //    LoadDataToGrid();
        //    //return;
            
        //}

    }


  

    protected void bntAddNew_Click(object sender, EventArgs e)
    {
        identity -= 1;
        txtMaChiTietThongTin.Text = identity.ToString();
        txtTenChiTietThongTin.Text = "";
        txtAlphaI.Text = "";
        txtGhiChu.Text = "";
        txtTenChiTietThongTin.Enabled = true;
        txtAlphaI.Enabled = true;
        txtGhiChu.Enabled = true;
    }
    protected void GridView_ChiTietThongTin_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if ((e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) && (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header))
        {
            //Giấu đi cột Mã Sản phẩm            
            e.Row.Cells[0].Visible = false;
            e.Row.Cells[2].Visible = false;
            e.Row.Cells[3].Visible = false;
        }
    }
    protected void bntXoa_Click(object sender, EventArgs e)
    {
        if (txtMaChiTietThongTin.Text.ToString().Trim().Length > 0 && Convert.ToInt32(txtMaChiTietThongTin.Text) > 0)
        {
            objCTTT = (ChiTietThongTin)Session["objChiTietThongTin_Temp"];
            dr = objCTTT.FindByPrimaryKey(txtMaChiTietThongTin.Text.ToString());
            dr.Delete();
            objCTTT.SaveData(Evaluation3.data.MyDataTable.DataTableAction.Delete);
            LoadDataToGrid();
            txtMaChiTietThongTin.Text = "";
            txtTenChiTietThongTin.Text = "";
            txtAlphaI.Text = "";
            txtGhiChu.Text = "";
            txtTenChiTietThongTin.Enabled = false;
            txtAlphaI.Enabled = false;
            txtGhiChu.Enabled = false;
        }
    }
    protected void GridView_ChiTietThongTin_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView_ChiTietThongTin.PageIndex = e.NewPageIndex;
        LoadDataToGrid();
    }
    protected void GridView_ChiTietToChuc_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView_ChiTietToChuc.PageIndex = e.NewPageIndex;
        LoadDataToGrid_TOCHUC();
    }
    protected void GridView_ChiTietToChuc_RowCommand(object sender, GridViewCommandEventArgs e)
    {
      try
      {
        if (e.CommandName == "Select")
        {

          int selectedrow = Convert.ToInt32(e.CommandArgument);
          objCTTC = (ChiTietToChuc)Session["objChiTietToChuc_Temp"];
          dr = objCTTC.FindByPrimaryKey(GridView_ChiTietToChuc.Rows[selectedrow].Cells[0].Text.ToString());
          txtMaChiTietToChuc.Text = dr["MaChiTietToChuc"].ToString();
          txtTenChiTietToChuc.Text = dr["TenChiTietToChuc"].ToString();
          txtAlphaO.Text = dr["AlphaO"].ToString();
          txt_O_GhiChu.Text = dr["GhiChu"].ToString();
          txtTenChiTietToChuc.Enabled = true;
          txtAlphaO.Enabled = true;
          txt_O_GhiChu.Enabled = true;
          //////int selectedrow = Convert.ToInt32(GridView_ChiTietThongTin.SelectedIndex);
          ////int selectedrow = Convert.ToInt32(e.CommandArgument);
          //////txtTenChiTietThongTin.Text = GridView_ChiTietThongTin.Rows[selectedrow].Cells[1].;
          ////txtMaChiTietToChuc.Value = GridView_ChiTietThongTin.Rows[selectedrow].Cells[0].Text.ToString();
          ////txtTenChiTietThongTin.Text = GridView_ChiTietThongTin.Rows[selectedrow].Cells[1].Text.ToString();
          ////txtAlphaI.Text = GridView_ChiTietThongTin.Rows[selectedrow].Cells[4].Text.ToString();
          ////txtGhiChu.Text=  GridView_ChiTietThongTin.Rows[selectedrow].Cells[5].Text.ToString();

        }
      }
      catch (Exception eRow3)
      {

      }
    }
    protected void bnt_O_CapNhat_Click(object sender, EventArgs e)
    {
      try
      {
        if (txtTenChiTietToChuc.Text == null || txtTenChiTietToChuc.Text.Trim().Length < 1)
        {
          //new ShowMessageBox("vao day ?");
          lblTenChiTietToChuc.Text = "* Tên chi tiết tổ chức không được để trống!";
          lblTenChiTietToChuc.Visible = true;
          txtTenChiTietToChuc.Focus();
          return;
        }
        else
        {
          lblTenChiTietToChuc.Visible = false;
        }
        if (txtAlphaO.Text != null)
        {
          try
          {
            double I = 0;
            I = Convert.ToDouble(txtAlphaO.Text);
            if (I < 0 || I > 1)
            {
              lblAlphaO.Text = "* Điểm Alpha O phải trong khoảng từ 0 đến 1!";
              lblAlphaO.Visible = true;
              txtAlphaO.Focus();
              return;
            }
            else
            {
              lblAlphaO.Visible = false;
            }
          }
          catch (Exception eConvert)
          {
            lblAlphaO.Text = "* Điểm Alpha O phải là kiểu số!";
            lblAlphaO.Visible = true;
            txtAlphaO.Focus();
            //Console.WriteLine(eConvert.Message);
            return;

          }

        }
        else if (txtAlphaO.Text == null)
        {
          lblAlphaO.Text = "* Điểm Alpha O không được để trống!";
          lblAlphaO.Visible = true;
          txtAlphaO.Focus();
          return;
        }
        else
        {
          lblAlphaO.Visible = false;
        }
        if (txtMaChiTietToChuc.Text.ToString().Trim().Length > 0 && Convert.ToInt32(txtMaChiTietToChuc.Text) > 0)
        {

          objCTTC = (ChiTietToChuc)Session["objChiTietToChuc_Temp"];
          dr = objCTTC.FindByPrimaryKey(txtMaChiTietToChuc.Text.ToString());
          //dr["MaChiTietThongTin"]=
          dr["TenChiTietToChuc"] = txtTenChiTietToChuc.Text;
          dr["AlphaO"] = txtAlphaO.Text;
          dr["GhiChu"] = txt_O_GhiChu.Text;
          objCTTC.SaveData(Evaluation3.data.MyDataTable.DataTableAction.AllChanges);
        }
        if (txtMaChiTietToChuc.Text.ToString().Trim().Length > 0 && Convert.ToInt32(txtMaChiTietToChuc.Text) < 0)
        {
          identity -= 1;
          objCTTC = (ChiTietToChuc)Session["objChiTietToChuc_Temp"];
          dr = objCTTC.NewRow();
          dr["MaChiTietToChuc"] = identity;
          dr["TenChiTietToChuc"] = txtTenChiTietToChuc.Text;
          dr["AlphaO"] = txtAlphaO.Text;
          dr["MaSanPham"] = MaSanPham;
          dr["MaHoiDong"] = MaHoiDong;
          dr["GhiChu"] = txt_O_GhiChu.Text;
          objCTTC.Rows.Add(dr);
          objCTTC.SaveData(Evaluation3.data.MyDataTable.DataTableAction.AllChanges);
          //Session["objChiTietThongTin_Temp"] = objCTTT;

        }

        LoadDataToGrid_TOCHUC();
        txtMaChiTietToChuc.Text = "";
        txtTenChiTietToChuc.Text = "";
        txtAlphaO.Text = "";
        txt_O_GhiChu.Text = "";
        txtTenChiTietToChuc.Enabled = false;
        txtAlphaO.Enabled = false;
        txt_O_GhiChu.Enabled = false;
      }
      catch (Exception eRow4)
      {

      }
    }
    protected void bnt_O_Xoa_Click(object sender, EventArgs e)
    {
        if (txtMaChiTietToChuc.Text.ToString().Trim().Length > 0 && Convert.ToInt32(txtMaChiTietToChuc.Text) > 0)
        {

            objCTTC = (ChiTietToChuc)Session["objChiTietToChuc_Temp"];
            dr = objCTTC.FindByPrimaryKey(txtMaChiTietToChuc.Text.ToString());
            dr.Delete();
            objCTTC.SaveData(Evaluation3.data.MyDataTable.DataTableAction.Delete);
            LoadDataToGrid_TOCHUC();
            txtMaChiTietToChuc.Text = "";
            txtTenChiTietToChuc.Text = "";
            txtAlphaO.Text = "";
            txt_O_GhiChu.Text = "";
            txtTenChiTietToChuc.Enabled = false;
            txtAlphaO.Enabled = false;
            txt_O_GhiChu.Enabled = false;
        }
    }
    protected void bntAddNewO_Click(object sender, EventArgs e)
    {
        identity -= 1;
        txtMaChiTietToChuc.Text = identity.ToString();
        txtTenChiTietToChuc.Text = "";
        txtAlphaO.Text = "";
        txt_O_GhiChu.Text = "";
        txtTenChiTietToChuc.Enabled = true;
        txtAlphaO.Enabled = true;
        txt_O_GhiChu.Enabled = true;
    }
    protected void bntAddNewMT_Click(object sender, EventArgs e)
    {
        identity -= 1;
        txtMaNhanToMoiTruong.Text = identity.ToString();
        txtTenNhanToMoiTruong.Text = "";
        txtLamda.Text = "";
        txt_MT_GhiChu.Text = "";
        txtTenNhanToMoiTruong.Enabled = true;
        txtLamda.Enabled = true;
        txt_MT_GhiChu.Enabled = true;
    }
    protected void bnt_MT_CapNhat_Click(object sender, EventArgs e)
    {
      try
      {
        if (txtTenNhanToMoiTruong.Text == null || txtTenNhanToMoiTruong.Text.Trim().Length < 1)
        {
          //new ShowMessageBox("vao day ?");
          lblTenNhanToMoiTruong.Text = "* Tên nhân tố môi trường không được để trống!";
          lblTenNhanToMoiTruong.Visible = true;
          txtTenNhanToMoiTruong.Focus();
          return;
        }
        else
        {
          lblTenNhanToMoiTruong.Visible = false;
        }
        if (txtLamda.Text != null)
        {
          try
          {
            double I = 0;
            I = Convert.ToDouble(txtLamda.Text);
            if (I < 0 || I > 1)
            {
              lblLamda.Text = "* Lamda phải trong khoảng từ 0 đến 1!";
              lblLamda.Visible = true;
              txtLamda.Focus();
              return;
            }
            else
            {
              lblLamda.Visible = false;
            }
          }
          catch (Exception eConvert)
          {
            lblLamda.Text = "* Lamda phải là kiểu số!";
            lblLamda.Visible = true;
            txtLamda.Focus();
            //Console.WriteLine(eConvert.Message);
            return;

          }

        }
        else if (txtLamda.Text == null)
        {
          lblLamda.Text = "* Lamda không được để trống!";
          lblLamda.Visible = true;
          txtLamda.Focus();
          return;
        }
        else
        {
          lblLamda.Visible = false;
        }
        if (txtMaNhanToMoiTruong.Text.ToString().Trim().Length > 0 && Convert.ToInt32(txtMaNhanToMoiTruong.Text) > 0)
        {


          objNTMT = (NhanToMoiTruong)Session["objNhanToMoiTruong_Temp"];
          dr = objNTMT.FindByPrimaryKey(txtMaNhanToMoiTruong.Text.ToString());
          //dr["MaChiTietThongTin"]=
          dr["TenNhanToMoiTruong"] = txtTenNhanToMoiTruong.Text;
          dr["Lamda"] = txtLamda.Text;
          dr["GhiChu"] = txt_MT_GhiChu.Text;
          objNTMT.SaveData(Evaluation3.data.MyDataTable.DataTableAction.AllChanges);
        }
        if (txtMaNhanToMoiTruong.Text.ToString().Trim().Length > 0 && Convert.ToInt32(txtMaNhanToMoiTruong.Text) < 0)
        {
          identity -= 1;
          objNTMT = (NhanToMoiTruong)Session["objNhanToMoiTruong_Temp"];
          dr = objNTMT.NewRow();
          dr["MaNhanToMoiTruong"] = identity;
          dr["TenNhanToMoiTruong"] = txtTenNhanToMoiTruong.Text;
          dr["Lamda"] = txtLamda.Text;
          dr["MaSanPham"] = MaSanPham;
          dr["MaHoiDong"] = MaHoiDong;
          dr["GhiChu"] = txt_MT_GhiChu.Text;
          objNTMT.Rows.Add(dr);
          objNTMT.SaveData(Evaluation3.data.MyDataTable.DataTableAction.AllChanges);
          //Session["objChiTietThongTin_Temp"] = objCTTT;

        }

        LoadDataToGrid_MOITRUONG();
        txtMaNhanToMoiTruong.Text = "";
        txtTenNhanToMoiTruong.Text = "";
        txtLamda.Text = "";
        txt_MT_GhiChu.Text = "";
        txtTenNhanToMoiTruong.Enabled = false;
        txtLamda.Enabled = false;
        txt_MT_GhiChu.Enabled = false;
      }
      catch (Exception eRow6)
      {

      }
    }
    protected void bnt_MT_Xoa_Click(object sender, EventArgs e)
    {
        if (txtMaNhanToMoiTruong.Text.ToString().Trim().Length > 0 && Convert.ToInt32(txtMaNhanToMoiTruong.Text) > 0)
        {

            objNTMT = (NhanToMoiTruong)Session["objNhanToMoiTruong_Temp"];
            dr = objNTMT.FindByPrimaryKey(txtMaNhanToMoiTruong.Text.ToString());
            dr.Delete();
            objNTMT.SaveData(Evaluation3.data.MyDataTable.DataTableAction.Delete);
            LoadDataToGrid_MOITRUONG();
            txtMaNhanToMoiTruong.Text = "";
            txtTenNhanToMoiTruong.Text = "";
            txtLamda.Text = "";
            txt_MT_GhiChu.Text = "";
            txtTenNhanToMoiTruong.Enabled = false;
            txtLamda.Enabled = false;
            txt_MT_GhiChu.Enabled = false;
        }
    }
    protected void GridView_NhanToMoiTruong_RowCommand(object sender, GridViewCommandEventArgs e)
    {
      try
      {
        if (e.CommandName == "Select")
        {

          int selectedrow = Convert.ToInt32(e.CommandArgument);
          objNTMT = (NhanToMoiTruong)Session["objNhanToMoiTruong_Temp"];
          dr = objNTMT.FindByPrimaryKey(GridView_NhanToMoiTruong.Rows[selectedrow].Cells[0].Text.ToString());
          txtMaNhanToMoiTruong.Text = dr["MaNhanToMoiTruong"].ToString();
          txtTenNhanToMoiTruong.Text = dr["TenNhanToMoiTruong"].ToString();
          txtLamda.Text = dr["Lamda"].ToString();
          txt_MT_GhiChu.Text = dr["GhiChu"].ToString();
          txtTenNhanToMoiTruong.Enabled = true;
          txtLamda.Enabled = true;
          txt_MT_GhiChu.Enabled = true;
        }
      }
      catch (Exception eRow5)
      {

      }
    }
    protected void GridView_NhanToMoiTruong_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView_NhanToMoiTruong.PageIndex = e.NewPageIndex;
        LoadDataToGrid_MOITRUONG();
    }
    protected void GridView_ChiTietToChuc_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if ((e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) && (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header))
        {
            //Giấu đi cột Mã Sản phẩm            
            e.Row.Cells[0].Visible = false;
            e.Row.Cells[2].Visible = false;
            e.Row.Cells[3].Visible = false;
        }
    }
    protected void GridView_NhanToMoiTruong_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if ((e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) && (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header))
        {
            //Giấu đi cột Mã Sản phẩm            
            e.Row.Cells[0].Visible = false;
            e.Row.Cells[2].Visible = false;
            e.Row.Cells[3].Visible = false;
        }
    }
  protected void cboMaThietBi_SelectedIndexChanged(object sender, EventArgs e)
  {
    LoadDataToGrid_BUOCGIACONG(cboMaThietBi.SelectedValue.ToString());
  }
  protected void bntAddNew_T_Click(object sender, EventArgs e)
  {
    identity -= 1;
    txtMaBuocGiaCong.Text = identity.ToString();
    txtTenBuocGiaCong.Text = "";
    txtAlPhaT.Text = "";
    txtGhiChu_T.Text = "";
    txtTenBuocGiaCong.Enabled = true;
    txtAlPhaT.Enabled = true;
    txtGhiChu_T.Enabled = true;
    cboMaThietBi.Enabled = true;
  }
  protected void bntSave_T_Click(object sender, EventArgs e)
  {
    try
    {
      if (cboMaThietBi.SelectedIndex == 0)
      {
        lblMaThietBi.Text = "Vui lòng chọn thiết bị!";
        lblMaThietBi.Visible = true;
        return;
      }
      else
      {
        lblMaThietBi.Visible = false;
      }
      if (txtTenBuocGiaCong.Text == null || txtTenBuocGiaCong.Text.Trim().Length < 1)
      {
        //new ShowMessageBox("vao day ?");
        lblTenBuocGiaCong.Text = "* Tên bước gia công không được để trống!";
        lblTenBuocGiaCong.Visible = true;
        txtTenBuocGiaCong.Focus();
        return;
      }
      else
      {
        lblTenBuocGiaCong.Visible = false;
      }
      if (txtAlPhaT.Text != null)
      {
        try
        {
          double I = 0;
          I = Convert.ToDouble(txtAlPhaT.Text);
          if (I < 0 || I > 1)
          {
            lblAlphaT.Text = "* Alpha T phải trong khoảng từ 0 đến 1!";
            lblAlphaT.Visible = true;
            txtAlPhaT.Focus();
            return;
          }
          else
          {
            lblAlphaT.Visible = false;
          }
        }
        catch (Exception eConvert)
        {
          lblAlphaT.Text = "* Alpha T phải là kiểu số!";
          lblAlphaT.Visible = true;
          txtAlPhaT.Focus();
          //Console.WriteLine(eConvert.Message);
          return;

        }

      }
      else if (txtAlPhaT.Text == null)
      {
        lblAlphaT.Text = "* Alpha T không được để trống!";
        lblAlphaT.Visible = true;
        txtAlPhaT.Focus();
        return;
      }
      else
      {
        lblAlphaT.Visible = false;
      }
      if (txtMaBuocGiaCong.Text.ToString().Trim().Length > 0 && Convert.ToInt32(txtMaBuocGiaCong.Text) > 0)
      {


        objBGC = (BuocGiaCong)Session["objBuocGiaCong_Temp"];
        dr = objBGC.FindByPrimaryKey(txtMaBuocGiaCong.Text.ToString());
        //dr["MaChiTietThongTin"]=
        dr["TenBuocGiaCong"] = txtTenBuocGiaCong.Text;
        dr["AlPhaT"] = txtAlPhaT.Text;
        dr["GhiChu"] = txtGhiChu_T.Text;
        objBGC.SaveData(Evaluation3.data.MyDataTable.DataTableAction.AllChanges);
      }
      if (txtMaBuocGiaCong.Text.ToString().Trim().Length > 0 && Convert.ToInt32(txtMaBuocGiaCong.Text) < 0)
      {
        identity -= 1;
        objBGC = (BuocGiaCong)Session["objBuocGiaCong_Temp"];
        dr = objBGC.NewRow();
        dr["MaBuocGiaCong"] = identity;
        dr["TenBuocGiaCong"] = txtTenBuocGiaCong.Text;
        dr["AlPhaT"] = txtAlPhaT.Text;
        dr["MaSanPham"] = MaSanPham;
        dr["MaHoiDong"] = MaHoiDong;
        dr["MaThietBi"] = cboMaThietBi.SelectedValue.ToString();
        dr["GhiChu"] = txtGhiChu_T.Text;
        objBGC.Rows.Add(dr);
        objBGC.SaveData(Evaluation3.data.MyDataTable.DataTableAction.AllChanges);
        //Session["objChiTietThongTin_Temp"] = objCTTT;

      }

      LoadDataToGrid_BUOCGIACONG(cboMaThietBi.SelectedValue.ToString());
      txtMaBuocGiaCong.Text = "";
      txtTenBuocGiaCong.Text = "";
      txtAlPhaT.Text = "";
      txtGhiChu_T.Text = "";
      txtTenBuocGiaCong.Enabled = false;
      txtAlPhaT.Enabled = false;
      txtGhiChu_T.Enabled = false;
    }
    catch (Exception eRow8)
    {

    }
  }
  protected void GridView_BuocGiaCong_RowCommand(object sender, GridViewCommandEventArgs e)
  {
    try
    {
      if (e.CommandName == "Select")
      {

        int selectedrow = Convert.ToInt32(e.CommandArgument);
        objBGC = (BuocGiaCong)Session["objBuocGiaCong_Temp"];
        dr = objBGC.FindByPrimaryKey(GridView_BuocGiaCong.Rows[selectedrow].Cells[0].Text.ToString());
        txtMaBuocGiaCong.Text = dr["MaBuocGiaCong"].ToString();
        txtTenBuocGiaCong.Text = dr["TenBuocGiaCong"].ToString();
        txtAlPhaT.Text = dr["AlPhaT"].ToString();
        txtGhiChu_T.Text = dr["GhiChu"].ToString();
        txtTenBuocGiaCong.Enabled = true;
        txtAlPhaT.Enabled = true;
        txtGhiChu_T.Enabled = true;
        cboMaThietBi.Enabled = false;
      }
    }
    catch (Exception eRow7)
    {

    }
  }
  protected void bntDelete_T_Click(object sender, EventArgs e)
  {
    if (txtMaBuocGiaCong.Text.ToString().Trim().Length > 0 && Convert.ToInt32(txtMaBuocGiaCong.Text) > 0)
    {

      objBGC = (BuocGiaCong)Session["objBuocGiaCong_Temp"];
      dr = objBGC.FindByPrimaryKey(txtMaBuocGiaCong.Text.ToString());
      dr.Delete();
      objBGC.SaveData(Evaluation3.data.MyDataTable.DataTableAction.Delete);
      LoadDataToGrid_BUOCGIACONG(cboMaThietBi.SelectedValue.ToString());
      txtMaBuocGiaCong.Text = "";
      txtTenBuocGiaCong.Text = "";
      txtAlPhaT.Text = "";
      txtGhiChu_T.Text = "";
      txtTenBuocGiaCong.Enabled = false;
      txtAlPhaT.Enabled = false;
      txtGhiChu_T.Enabled = false;
    }
  }
  protected void GridView_BuocGiaCong_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    if ((e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) && (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header))
    {
      //Giấu đi cột Mã Sản phẩm            
      e.Row.Cells[0].Visible = false;
      e.Row.Cells[2].Visible = false;
      e.Row.Cells[3].Visible = false;
      e.Row.Cells[6].Visible = false;
    }
  }
  protected void GridView_BuocGiaCong_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {
    GridView_NhanToMoiTruong.PageIndex = e.NewPageIndex;
    LoadDataToGrid_BUOCGIACONG(cboMaThietBi.SelectedValue.ToString());
  }
  protected void bntAddNew_HSTL_Click(object sender, EventArgs e)
  {
    identity -= 1;
    txtMaHeSoTrongLuong_SanPham.Text = identity.ToString();
    txtTenHeSoTrongLuong.Text = "";
    txtDiem.Text = "";
    txtGhiChu_HSTL.Text = "";
    txtTenHeSoTrongLuong.Enabled = true;
    txtDiem.Enabled = true;
    txtGhiChu_HSTL.Enabled = true;
  }
  protected void bntSave_HSTL_Click(object sender, EventArgs e)
  {
    try
    {
      if (txtTenHeSoTrongLuong.Text == null || txtTenHeSoTrongLuong.Text.Trim().Length < 1)
      {
        //new ShowMessageBox("vao day ?");
        lblTenHeSoTrongLuong.Text = "* Tên hệ số trọng lượng không được để trống!";
        lblTenHeSoTrongLuong.Visible = true;
        txtTenHeSoTrongLuong.Focus();
        return;
      }
      else
      {
        lblTenHeSoTrongLuong.Visible = false;
      }
      if (txtDiem.Text != null)
      {
        try
        {
          double I = 0;
          I = Convert.ToDouble(txtDiem.Text);
          if (I < 0 || I > 1)
          {
            lblDiem.Text = "* Điểm phải trong khoảng từ 0 đến 1!";
            lblDiem.Visible = true;
            txtDiem.Focus();
            return;
          }
          else
          {
            lblDiem.Visible = false;
          }
        }
        catch (Exception eConvert)
        {
          lblDiem.Text = "* Điểm phải là kiểu số!";
          lblDiem.Visible = true;
          txtDiem.Focus();
          //Console.WriteLine(eConvert.Message);
          return;

        }

      }
      else if (txtDiem.Text == null)
      {
        lblDiem.Text = "* Điểm không được để trống!";
        lblDiem.Visible = true;
        txtDiem.Focus();
        return;
      }
      else
      {
        lblDiem.Visible = false;
      }
      if (txtMaHeSoTrongLuong_SanPham.Text.ToString().Trim().Length > 0 && Convert.ToInt32(txtMaHeSoTrongLuong_SanPham.Text) > 0)
      {

        objHSTL = (HeSoTrongLuong)Session["objHeSoTrongLuong_Temp"];
        dr = objHSTL.FindByPrimaryKey(txtMaHeSoTrongLuong_SanPham.Text.ToString());
        //dr["MaChiTietThongTin"]=
        dr["TenHeSoTrongLuong"] = txtTenHeSoTrongLuong.Text;
        dr["Diem"] = txtDiem.Text;
        dr["GhiChu"] = txtGhiChu_HSTL.Text;
        objHSTL.SaveData(Evaluation3.data.MyDataTable.DataTableAction.AllChanges);
      }
      if (txtMaHeSoTrongLuong_SanPham.Text.ToString().Trim().Length > 0 && Convert.ToInt32(txtMaHeSoTrongLuong_SanPham.Text) < 0)
      {
        identity -= 1;
        objHSTL = (HeSoTrongLuong)Session["objHeSoTrongLuong_Temp"];
        dr = objHSTL.NewRow();
        dr["MaHeSoTrongLuong_SanPham"] = identity;
        dr["TenHeSoTrongLuong"] = txtTenHeSoTrongLuong.Text;
        dr["Diem"] = txtDiem.Text;
        dr["MaHoiDong"] = MaHoiDong;
        dr["GhiChu"] = txtGhiChu_HSTL.Text;
        objHSTL.Rows.Add(dr);
        objHSTL.SaveData(Evaluation3.data.MyDataTable.DataTableAction.AllChanges);
        //Session["objChiTietThongTin_Temp"] = objCTTT;

      }

      LoadDataToGrid_HESOTRONGLUONG();
      txtMaHeSoTrongLuong_SanPham.Text = "";
      txtTenHeSoTrongLuong.Text = "";
      txtDiem.Text = "";
      txtGhiChu_HSTL.Text = "";
      txtTenHeSoTrongLuong.Enabled = false;
      txtDiem.Enabled = false;
      txtGhiChu_HSTL.Enabled = false;
    }
    catch (Exception eRow10)
    {

    }
  }
  protected void bnDelete_HSTL_Click(object sender, EventArgs e)
  {
    if (txtMaHeSoTrongLuong_SanPham.Text.ToString().Trim().Length > 0 && Convert.ToInt32(txtMaHeSoTrongLuong_SanPham.Text) > 0)
    {

      objHSTL = (HeSoTrongLuong)Session["objHeSoTrongLuong_Temp"];
      dr = objHSTL.FindByPrimaryKey(txtMaHeSoTrongLuong_SanPham.Text.ToString());
      dr.Delete();
      objHSTL.SaveData(Evaluation3.data.MyDataTable.DataTableAction.Delete);
      LoadDataToGrid_HESOTRONGLUONG();
      txtMaHeSoTrongLuong_SanPham.Text = "";
      txtTenHeSoTrongLuong.Text = "";
      txtDiem.Text = "";
      txtGhiChu_HSTL.Text = "";
      txtTenHeSoTrongLuong.Enabled = false;
      txtDiem.Enabled = false;
      txtGhiChu_HSTL.Enabled = false;
    }
  }
  protected void GridView_HeSoTrongLuong_RowCommand(object sender, GridViewCommandEventArgs e)
  {
    try
    {
      if (e.CommandName == "Select")
      {

        int selectedrow = Convert.ToInt32(e.CommandArgument);
        objHSTL = (HeSoTrongLuong)Session["objHeSoTrongLuong_Temp"];
        dr = objHSTL.FindByPrimaryKey(GridView_HeSoTrongLuong.Rows[selectedrow].Cells[0].Text.ToString());
        txtMaHeSoTrongLuong_SanPham.Text = dr["MaHeSoTrongLuong_SanPham"].ToString();
        txtTenHeSoTrongLuong.Text = dr["TenHeSoTrongLuong"].ToString();
        txtDiem.Text = dr["Diem"].ToString();
        txtGhiChu_HSTL.Text = dr["GhiChu"].ToString();
        txtTenHeSoTrongLuong.Enabled = true;
        txtDiem.Enabled = true;
        txtGhiChu_HSTL.Enabled = true;
      }
    }
    catch (Exception eRow9)
    {

    }
  }
  protected void GridView_HeSoTrongLuong_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    if ((e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) && (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header))
    {
      //Giấu đi cột Mã Sản phẩm            
      e.Row.Cells[0].Visible = false;
      e.Row.Cells[4].Visible = false;
      
    }
  }
  protected void GridView_HeSoTrongLuong_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {
    GridView_HeSoTrongLuong.PageIndex = e.NewPageIndex;
    LoadDataToGrid_HESOTRONGLUONG();
  }
    protected void ASPxPageControl1_ActiveTabChanged(object source, DevExpress.Web.ASPxTabControl.TabControlEventArgs e)
    {

    }
}

